package com.atguigu.guli.service.edu.controller.admin;

import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.entity.TeacherQuery;
import com.atguigu.guli.service.edu.feign.OssFileService;
import com.atguigu.guli.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 讲师管理
 */
@Api(tags = "讲师管理模块")
@RestController
@RequestMapping("/admin/edu/teacher/")

public class AdminTeacherController {
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private OssFileService ossFileService;

    @ApiOperation("所有讲师列表")
    //查询所有讲师
    @GetMapping("/list")
    public R getList() {
        List<Teacher> teachers = teacherService.list();
        return R.ok().data("items", teachers).message("获取讲师列表成功");
    }

    @ApiOperation("根据ID查询讲师")
    //根据ID查询讲师
    @GetMapping("/getById/{id}")
    public R getById(@ApiParam(value = "讲师ID") @PathVariable("id") String id) {
        Teacher teacher = teacherService.getById(id);
        //int i = 10/0;
        return R.ok().data("item", teacher);
    }

    //添加讲师
    @ApiOperation("新增讲师")
    @PostMapping("/save")
    public R Save(@RequestBody Teacher teacher) {
        boolean save = teacherService.save(teacher);
        return R.ok().message(save ? "新增成功！" : "新增失败...");
    }

    //修改讲师
    @CacheEvict(value = "ads",key = "'cache'")
    @ApiOperation("修改讲师")
    @PutMapping("/updateById")
    public R updateById(@RequestBody Teacher teacher) {
        //Teacher teacher = teacherService.getById(id);
        boolean update = teacherService.updateById(teacher);
        return R.ok().message(update?"更新成功！":"更新失败...........");
    }

    //删除讲师
    @ApiOperation(value = "删除讲师", notes = "根据ID删除讲师")
    @DeleteMapping("/deleteById/{id}")
    public R Delete(@ApiParam(value = "讲师ID") @PathVariable("id") String id) {
        //删除讲师之前先可以查询讲师信息将头像地址存到缓存中
        Teacher teacher = teacherService.getById(id);
        boolean remove = teacherService.removeById(id);
        if (remove &&! StringUtils.isEmpty(teacher.getAvatar())){//判断是否删除成功且讲师头像不为空
            ossFileService.delete(teacher.getAvatar(), "avatar");
        }
        return R.ok();
    }
    //批量删除讲师
    @DeleteMapping("/batchDel")
    public R bathDel(@RequestBody List<String> ids){
        teacherService.removeByIds(ids);
        return R.ok();
    }

    //@ApiOperation("分页查询讲师")
    @ApiOperation("带条件分页查询讲师")
    //V1 分页查询讲师
    //V2 带条件的分页查询
    @GetMapping("/pageList/{pageNum}/{pageSize}")
    public R pageList(@ApiParam("当前页码") @PathVariable Integer pageNum
            ,@ApiParam("每页记录条数") @PathVariable Integer pageSize,
              @ApiParam("分页条件")       TeacherQuery teacherQuery) {
        //不带条件的查询
        //Page<Teacher> page = new Page<>(pageNum, pageSize);
        //Page<Teacher> pageInfo = teacherService.page(page);
        //return R.ok().data("page", pageInfo);

        //带条件的分页查询
        Page<Teacher> page = teacherService.getPage(new Page<>(pageNum, pageSize), teacherQuery);
        return R.ok().data("page",page);
    }
    @ApiOperation("测试服务调用")
    @GetMapping("/test")
    public R test(){
        ossFileService.test();

        //try {
        //    TimeUnit.SECONDS.sleep(3);
        //} catch (InterruptedException e) {
        //    e.printStackTrace();
        //}
        return R.ok();
    }


}
